from fastapi import APIRouter, Depends

from app.auth.models.api import ApiCreate
from app.auth.models.app_model import AppModelCreate, AppModelPublic
from app.auth.models.token import RefreshTokenRequest, TokenResponse
from app.auth.services import ApiService, AppModelService
from app.auth.services.token import TokenService
from app.core.handlers.response import BaseResponse
from app.user.models.user import UserLogin

auth_router = APIRouter(prefix="/auth", tags=["Auth"])


@auth_router.post(
    "/login", summary="用户登录", response_model=BaseResponse[TokenResponse]
)
async def login(form_data: UserLogin, token_service: TokenService = Depends()):
    return await token_service.login(form_data)


@auth_router.post(
    "/refresh", summary="刷新令牌", response_model=BaseResponse[TokenResponse]
)
async def refresh_token(
    refresh_request: RefreshTokenRequest, token_service: TokenService = Depends()
):
    return await token_service.refresh_token(refresh_request.refresh_token)


@auth_router.post("/api", summary="新增API权限", response_model=BaseResponse)
async def create_api(api_data: ApiCreate, api_service: ApiService = Depends()):
    api = await api_service.create_api(api_data)
    return api


@auth_router.post(
    "/app_model",
    summary="新增AppModel",
    response_model=BaseResponse[AppModelPublic],
    description="创建新的AppModel记录，用于权限系统",
)
async def create_app_model(
    app_model_data: AppModelCreate, app_model_service: AppModelService = Depends()
):
    app_model = await app_model_service.create_app_model(app_model_data)
    return app_model
